home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Kant Generator Pro 1.2 / src / kode ƒ / kant main window.c < prev    next >
C/C++ Source or Header  |  1995-02-13  |  6KB  |  241 lines

  1. #include "kant main window.h"
  2. #include "kant load-save.h"
  3. #include "drag utilities.h"
  4. #include "environment.h"
  5. #include "util.h"
  6. #include "menus.h"
  7. #include "main.h"
  8. #include "dialogs.h"
  9. #include "text twiddling.h"
  10. #include "generic window handlers.h"
  11. #include "window layer.h"
  12. #include "program globals.h"
  13.  
  14. #define kGrowBoxSize        15
  15.  
  16. static    void PutDataIntoTEFields(WindowPtr theWindow);
  17.  
  18. static    short            gOldForegroundTime;        /* stored foreground wait time */
  19. #if powerc
  20. static    TEClickLoopUPP    gClickLoopUPP;
  21. #endif
  22. static    Str255            gMainWindowTitle;
  23. static    Boolean            gSetupDone=FALSE;
  24. static    Boolean            gIsActive=FALSE;
  25.  
  26. void SetupTheMainWindow(WindowPtr theWindow)
  27. {
  28.     unsigned char    *titleStr="\puntitled";
  29.     Point            topLeft;
  30.     FSSpec            fs;
  31.     
  32.     SetWindowHeight(theWindow, qd.screenBits.bounds.bottom-qd.screenBits.bounds.top-LMGetMBarHeight()-28);
  33.     SetWindowWidth(theWindow, qd.screenBits.bounds.right-qd.screenBits.bounds.left-70);
  34.     SetWindowType(theWindow, zoomDocProc);
  35.     topLeft.v=qd.screenBits.bounds.top+LMGetMBarHeight()+20;
  36.     topLeft.h=qd.screenBits.bounds.left+10;
  37.     SetWindowTopLeft(theWindow, topLeft);
  38.     SetWindowHasCloseBox(theWindow, TRUE);
  39.     SetWindowMaxDepth(theWindow, 1);
  40.     SetWindowDepth(theWindow, 1);
  41.     SetWindowIsFloat(theWindow, FALSE);
  42.     if (gMainWindowTitle[0]==0x00)
  43.     {
  44.         SetWindowTitle(theWindow, titleStr);
  45.     }
  46.     else
  47.     {
  48.         SetWindowTitle(theWindow, gMainWindowTitle);
  49.     }
  50.     
  51.     SetWindowAutoCenter(theWindow, FALSE);
  52.     fs.name[0]=0x00;
  53.     fs.vRefNum=0;
  54.     fs.parID=0;
  55.     SetWindowFS(theWindow, fs);
  56.     SetWindowIsModified(theWindow, FALSE);
  57.     SetWindowIsDraggable(theWindow, TRUE);
  58.     SetWindowIsPrintable(theWindow, TRUE);
  59.     
  60.     if (gSetupDone)
  61.         return;
  62.     
  63.     gSetupDone=TRUE;
  64. #if powerc
  65.     gClickLoopUPP=NewTEClickLoopProc(ClickLoopProc);
  66. #endif
  67. }
  68.  
  69. void ShutDownTheMainWindow(void)
  70. {
  71. #if powerc
  72.     if (gClickLoopUPP!=0L)
  73.         DisposeRoutineDescriptor(gClickLoopUPP);
  74. #endif
  75. }
  76.  
  77. void OpenTheMainWindow(WindowPtr theWindow)
  78. {
  79.     TEHandle        hTE;
  80.     FontInfo        theFontInfo;
  81.     Rect            vScrollBarRect, hScrollBarRect;
  82.     Rect            destRect, viewRect;
  83.     
  84.     hTE=GetWindowTE(theWindow);
  85.     if (hTE==0L)
  86.     {
  87.         SetRect(&vScrollBarRect, GetWindowWidth(theWindow)-kGrowBoxSize, -1,
  88.             GetWindowWidth(theWindow)+1, GetWindowHeight(theWindow)+1-kGrowBoxSize);
  89.         SetRect(&hScrollBarRect, -1, GetWindowHeight(theWindow)-kGrowBoxSize,
  90.             GetWindowWidth(theWindow)-kGrowBoxSize+1, GetWindowHeight(theWindow)+1);
  91.         SetWindowVScrollBar(theWindow,
  92.             NewControl(theWindow, &vScrollBarRect, "\p", TRUE, 0, 0, 0, scrollBarProc, 0));
  93.         SetWindowHScrollBar(theWindow,
  94.             NewControl(theWindow, &hScrollBarRect, "\p", TRUE, 0, 0, 0, scrollBarProc, 0));
  95.         
  96.         GetTERect(theWindow, &destRect, TRUE);
  97.         viewRect=destRect;
  98.         hTE=TENew(&destRect, &viewRect);
  99.         SetWindowTE(theWindow, hTE);
  100.         TextFont((**hTE).txFont=monaco);
  101.         TextSize((**hTE).txSize=9);
  102.         TextFace((**hTE).txFace=0);
  103.         GetFontInfo(&theFontInfo);
  104.         (**hTE).fontAscent=theFontInfo.ascent;
  105.         (**hTE).lineHeight=theFontInfo.ascent+theFontInfo.descent+theFontInfo.leading;
  106.         AdjustViewRect(hTE);
  107.         TEAutoView(TRUE, hTE);
  108.         SetWindowOldClickLoopProc(theWindow, (**hTE).clickLoop);
  109. #if powerc
  110.         TESetClickLoop(gClickLoopUPP, hTE);
  111. #else
  112.         (**hTE).clickLoop=(TEClickLoopUPP)MyClikLoop;
  113. #endif
  114.         PutDataIntoTEFields(theWindow);
  115.     }
  116.     
  117.     gIsActive=TRUE;
  118.     AdjustVScrollBar(GetWindowVScrollBar(theWindow), hTE);
  119.     AdjustMenus();
  120. }
  121.  
  122. void KeyPressedInMainWindow(WindowPtr theWindow, unsigned char theChar)
  123. {
  124.     TEHandle        hTE;
  125.     ControlHandle    vScrollBar;
  126.     
  127.     hTE=GetWindowTE(theWindow);
  128.     vScrollBar=GetWindowVScrollBar(theWindow);
  129.     
  130.     switch (theChar)
  131.     {
  132.         default:
  133.             TEKey(theChar, hTE);
  134.             SetWindowIsModified(theWindow, TRUE);
  135.             SetWindowLastFindPosition(theWindow, (**hTE).selStart);
  136.             break;
  137.     }
  138.     
  139.     AdjustForEndScroll(vScrollBar, hTE);
  140.     AdjustVScrollBar(vScrollBar, hTE);
  141. }
  142.  
  143. void DisposeTheMainWindow(WindowPtr theWindow)
  144. {
  145.     TEHandle        hTE;
  146.     
  147.     hTE=GetWindowTE(theWindow);
  148.     if (hTE!=0L)
  149.     {
  150.         TEDispose(hTE);
  151.         SetWindowTE(theWindow, 0L);
  152.     }
  153. }
  154.  
  155. Boolean CloseTheMainWindow(WindowPtr theWindow)
  156. {
  157.     ModalFilterUPP    procFilter = NewModalFilterProc(ThreeButtonFilter);
  158.     short            result;
  159.     
  160.     if (WindowIsModifiedQQ(theWindow))
  161.     {
  162.         SetCursor(&qd.arrow);
  163.         PositionDialog('ALRT', saveAlert);
  164.         ParamText(GetWindowTitle(theWindow), "\p", "\p", "\p");
  165.         result=Alert(saveAlert, procFilter);
  166.         DisposeRoutineDescriptor(procFilter);
  167.         switch (result)
  168.         {
  169.             case 1:    /* save */
  170.                 LoadSaveDispatch(FALSE, TRUE);
  171.                 if (WindowIsModifiedQQ(theWindow))    /* save didn't work for some reason */
  172.                     return FALSE;
  173.                 break;
  174.             case 2:     /* cancel */
  175.                 return FALSE;
  176.                 break;
  177.             case 3:    /* don't save */
  178.                 break;
  179.         }
  180.     }
  181.     
  182.     gCustomCursor=FALSE;
  183.     
  184.     return TRUE;
  185. }
  186.  
  187. void PasteInMainWindow(WindowPtr theWindow)
  188. {
  189.     TEHandle        hTE;
  190.     ControlHandle    vScrollBar;
  191.     Handle            scrapHandle;
  192.     long            offset;
  193.     unsigned long    scrapLength;
  194.     unsigned char    *theSource="\pMBDF-A";
  195.     unsigned char    *theTarget="\pProgramming is not a crime.";
  196.     
  197.     hTE=GetWindowTE(theWindow);
  198.     vScrollBar=GetWindowVScrollBar(theWindow);
  199.     
  200.     scrapHandle=NewHandle(0L);
  201.     if (GetScrap(scrapHandle, 'TEXT', &offset)!=noTypeErr)
  202.     {
  203.         scrapLength=GetHandleSize(scrapHandle);
  204.         if (scrapLength==theSource[0])
  205.         {
  206.             HLock(scrapHandle);
  207.             if (Mymemcompare((Ptr)*scrapHandle, (Ptr)&theSource[1], theSource[0]))
  208.             {
  209.                 ZeroScrap();
  210.                 PutScrap(theTarget[0], 'TEXT', (char*)&theTarget[1]);
  211.             }
  212.         }
  213.         
  214.         TEFromScrap();
  215.         TEPaste(hTE);
  216.         TESelView(hTE);
  217.         if (vScrollBar!=0L)
  218.             AdjustVScrollBar(vScrollBar, hTE);
  219.         if (scrapLength!=0L)
  220.             SetWindowIsModified(theWindow, TRUE);
  221.     }
  222.     DisposeHandle(scrapHandle);
  223.     SetWindowLastFindPosition(theWindow, (**hTE).selStart);
  224.     ResetHiliteRgn(theWindow);
  225. }
  226.  
  227. static    void PutDataIntoTEFields(WindowPtr theWindow)
  228. {
  229.     TEHandle            hTE;
  230.     
  231.     hTE=GetWindowTE(theWindow);
  232.     TESetSelect(0, 0, hTE);
  233.     TEKey(0x00, hTE);
  234.     TEKey(0x08, hTE);
  235. }
  236.  
  237. void SetMainWindowTitle(Str255 theTitle)
  238. {
  239.     Mymemcpy((Ptr)gMainWindowTitle, (Ptr)theTitle, theTitle[0]+1);
  240. }
  241.